import React, { Component } from 'react'

export default class App extends Component {
  constructor(props) {
    super(props)

    this.state = {
      counter: 0
    }
  }

  render() {
    return (
      <div>
        <h2>当前计数：{this.state.counter}</h2>
        <button onClick={() => { this.increment() }}>+1</button>
        <button id="btn">改变</button>
      </div>
    )
  }
  componentDidMount() {
    document.getElementById("btn").addEventListener("click", () => {
      // 同步更新
      this.setState({
        counter: this.state.counter + 1
      })
      console.log("同步btn", this.state.counter)
    })
  }

  componentDidUpdate() {
    console.log("update", this.state.counter)
  }

  increment() {
    setTimeout(() => {
      // 同步更新
      this.setState({
        counter: this.state.counter + 1
      })
      console.log("同步", this.state.counter)
    }, 0)
    
  }
}
